<template>
  <div class="select-page">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back-btn" @click="goBack">
        <i class="arrow-left">←</i>
      </div>
      <div class="title">选择患者</div>
    </div>

    <!-- 搜索框 -->
    <div class="search-box">
      <input
        type="text"
        v-model="searchKeyword"
        placeholder="请输入患者相关信息"
        class="search-input"
      >
    </div>

    <!-- 患者列表 -->
    <div class="patient-list">
      <!-- 全选选项 -->
      <div class="patient-item">
        <label class="checkbox-wrapper">
          <input
            type="checkbox"
            v-model="selectAll"
            @change="handleSelectAll"
          >
          <span class="checkbox-label">全选</span>
          <span class="patient-count">{{ filteredPatients.length }}人</span>
        </label>
      </div>

      <!-- 患者列表 -->
      <div v-for="(patient, index) in filteredPatients"
           :key="index"
           class="patient-item">
        <label class="checkbox-wrapper">
          <input
            type="checkbox"
            v-model="selectedPatients"
            :value="patient.id"
          >
          <img :src="patient.avatar || defaultAvatar" class="avatar" alt="头像">
          <div class="patient-info">
            <div class="name-age">
              {{ patient.userName }} {{ patient.sex }} {{ patient.age }}岁
            </div>
            <div class="visit-time">就诊时间: {{ patient.createTime }}</div>
          </div>
          <span class="tag">专家门诊</span>
        </label>
      </div>
    </div>

    <!-- 底部操作栏 -->
    <div class="bottom-bar">
      <div class="selected-count">已选择{{ selectedPatients.length }}人</div>
      <div class="confirm-btn" @click="confirmSelection">确定</div>
    </div>

    <div v-if="loading" class="loading">
      加载中...
    </div>
  </div>
</template>

<script>
import { selectPatient } from "@/api/message/message";

export default {
  name: 'SelectPatients',
  data() {
    return {
      searchKeyword: '',
      patientList: [],
      selectedPatients: [],
      selectAll: false,
      defaultAvatar: require('@/assets/images/屏幕截图 2025-03-01 110826.png'),
      loading: false,
      id: null
    }
  },
  computed: {
    filteredPatients() {
      if (!this.searchKeyword) return this.patientList;
      const keyword = this.searchKeyword.toLowerCase();
      return this.patientList.filter(patient =>
        patient.userName.toLowerCase().includes(keyword)
      );
    }
  },
  created() {
    // 获取路由传递的分组ID
    this.id = this.$route.query.id;
    if (this.id) {
      this.getPatientList();
    }
  },
  methods: {
    async getPatientList() {
      try {
        this.loading = true;
        const response = await selectPatient(this.id);
        if (response.code === 200) {
          this.patientList = response.data || [];
        } else {
          this.$message.error(response.msg || '获取患者列表失败');
        }
      } catch (error) {
        console.error('获取患者列表失败:', error);
        this.$message.error('获取患者列表失败，请稍后重试');
      } finally {
        this.loading = false;
      }
    },
    goBack() {
      this.$router.go(-1);
    },
    handleSelectAll() {
      if (this.selectAll) {
        this.selectedPatients = this.filteredPatients.map(p => p.id);
      } else {
        this.selectedPatients = [];
      }
    },
    confirmSelection() {
      // 返回选中的患者到新增消息页面
      this.$router.push({
        path: '/message/add',
        query: {
          selectedPatients: JSON.stringify(
            this.patientList.filter(p => this.selectedPatients.includes(p.id))
          )
        }
      });
    }
  },
  watch: {
    searchKeyword(val) {
      // 当搜索关键字变化时，重新获取患者列表
      if (this.id) {
        this.getPatientList();
      }
    },
    selectedPatients(val) {
      // 更新全选状态
      this.selectAll = val.length === this.filteredPatients.length;
    }
  }
}
</script>

<style scoped lang="scss">
.select-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.nav-header {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #eee;

  .back-btn {
    font-size: 20px;
    color: #333;
    margin-right: 16px;
    cursor: pointer;
  }

  .title {
    font-size: 18px;
    font-weight: 500;
  }
}

.search-box {
  padding: 12px 16px;
  background-color: #fff;

  .search-input {
    width: 100%;
    height: 36px;
    background-color: #f5f5f5;
    border: none;
    border-radius: 18px;
    padding: 0 16px;
    font-size: 14px;

    &::placeholder {
      color: #999;
    }
  }
}

.patient-list {
  .patient-item {
    background-color: #fff;
    border-bottom: 1px solid #f5f5f5;

    .checkbox-wrapper {
      display: flex;
      align-items: center;
      padding: 12px 16px;
      cursor: pointer;

      input[type="checkbox"] {
        margin-right: 12px;
      }

      .checkbox-label {
        font-size: 14px;
        color: #333;
      }

      .patient-count {
        font-size: 14px;
        color: #999;
        margin-left: 8px;
      }

      .avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin-right: 12px;
      }

      .patient-info {
        flex: 1;

        .name-age {
          font-size: 14px;
          color: #333;
          margin-bottom: 4px;
        }

        .visit-time {
          font-size: 12px;
          color: #999;
        }
      }

      .tag {
        padding: 2px 8px;
        background-color: #fff7e6;
        color: #ff9500;
        border-radius: 2px;
        font-size: 12px;
      }
    }
  }
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background-color: #fff;
  border-top: 1px solid #eee;

  .selected-count {
    font-size: 14px;
    color: #666;
  }

  .confirm-btn {
    padding: 8px 24px;
    background-color: #409eff;
    color: #fff;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;

    &:active {
      background-color: #3a8ee6;
    }
  }
}

.loading {
  text-align: center;
  color: #999;
  padding: 32px 0;
}
</style>
